<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>java-spark-朴素贝叶斯算法(naive-bayes) | Teler的日常</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="博客小站，记录我的学习日常和心情等">
    <meta name="keywords" content="前端博客,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown">
    <meta name="baidu-site-verification" content="code-Rpieelje4H">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.bb5b1957.css" as="style"><link rel="preload" href="/assets/js/app.d41d74c0.js" as="script"><link rel="preload" href="/assets/js/2.023436e0.js" as="script"><link rel="preload" href="/assets/js/18.004165c1.js" as="script"><link rel="prefetch" href="/assets/js/10.f3664957.js"><link rel="prefetch" href="/assets/js/100.98bebc7f.js"><link rel="prefetch" href="/assets/js/101.fe34203c.js"><link rel="prefetch" href="/assets/js/102.ff08e3d5.js"><link rel="prefetch" href="/assets/js/103.7f0f328d.js"><link rel="prefetch" href="/assets/js/104.cf87ee60.js"><link rel="prefetch" href="/assets/js/105.a87694e9.js"><link rel="prefetch" href="/assets/js/106.a478e8b1.js"><link rel="prefetch" href="/assets/js/107.720dff4e.js"><link rel="prefetch" href="/assets/js/108.27f0de33.js"><link rel="prefetch" href="/assets/js/109.f567acfb.js"><link rel="prefetch" href="/assets/js/11.f2e49a76.js"><link rel="prefetch" href="/assets/js/110.9763d4af.js"><link rel="prefetch" href="/assets/js/111.db6527e1.js"><link rel="prefetch" href="/assets/js/112.a20bf841.js"><link rel="prefetch" href="/assets/js/113.fffb90e0.js"><link rel="prefetch" href="/assets/js/114.e10b299e.js"><link rel="prefetch" href="/assets/js/115.73b68794.js"><link rel="prefetch" href="/assets/js/116.a41be574.js"><link rel="prefetch" href="/assets/js/117.f49ddfb7.js"><link rel="prefetch" href="/assets/js/118.f31c49c3.js"><link rel="prefetch" href="/assets/js/119.e7ddd80c.js"><link rel="prefetch" href="/assets/js/12.684abe5a.js"><link rel="prefetch" href="/assets/js/120.ab58a792.js"><link rel="prefetch" href="/assets/js/121.8070a099.js"><link rel="prefetch" href="/assets/js/122.bd4aa83d.js"><link rel="prefetch" href="/assets/js/123.8b28ab66.js"><link rel="prefetch" href="/assets/js/124.c64e8a82.js"><link rel="prefetch" href="/assets/js/125.8c37e25d.js"><link rel="prefetch" href="/assets/js/126.7a965ec1.js"><link rel="prefetch" href="/assets/js/127.15e3f4d7.js"><link rel="prefetch" href="/assets/js/128.92e24736.js"><link rel="prefetch" href="/assets/js/129.376be23d.js"><link rel="prefetch" href="/assets/js/13.f2551a14.js"><link rel="prefetch" href="/assets/js/130.cde8392a.js"><link rel="prefetch" href="/assets/js/131.f7e13266.js"><link rel="prefetch" href="/assets/js/132.a518186c.js"><link rel="prefetch" href="/assets/js/133.d4a43462.js"><link rel="prefetch" href="/assets/js/134.95f419b0.js"><link rel="prefetch" href="/assets/js/135.07df0368.js"><link rel="prefetch" href="/assets/js/136.08277af4.js"><link rel="prefetch" href="/assets/js/137.3c3b9976.js"><link rel="prefetch" href="/assets/js/138.1e1bb8ce.js"><link rel="prefetch" href="/assets/js/139.78d61366.js"><link rel="prefetch" href="/assets/js/14.4fac3ae1.js"><link rel="prefetch" href="/assets/js/15.c4e1a86c.js"><link rel="prefetch" href="/assets/js/16.08f9cfab.js"><link rel="prefetch" href="/assets/js/17.40f3d3ac.js"><link rel="prefetch" href="/assets/js/19.71a91c37.js"><link rel="prefetch" href="/assets/js/20.be80e063.js"><link rel="prefetch" href="/assets/js/21.dd2e712d.js"><link rel="prefetch" href="/assets/js/22.efedd1da.js"><link rel="prefetch" href="/assets/js/23.b8d5f0c5.js"><link rel="prefetch" href="/assets/js/24.8895cb39.js"><link rel="prefetch" href="/assets/js/25.f72d16f6.js"><link rel="prefetch" href="/assets/js/26.700b8757.js"><link rel="prefetch" href="/assets/js/27.2d888c7a.js"><link rel="prefetch" href="/assets/js/28.c95a06e3.js"><link rel="prefetch" href="/assets/js/29.030c41dc.js"><link rel="prefetch" href="/assets/js/3.ae79ad07.js"><link rel="prefetch" href="/assets/js/30.024d7386.js"><link rel="prefetch" href="/assets/js/31.90222925.js"><link rel="prefetch" href="/assets/js/32.98d886f9.js"><link rel="prefetch" href="/assets/js/33.fdc186c5.js"><link rel="prefetch" href="/assets/js/34.c6364dc9.js"><link rel="prefetch" href="/assets/js/35.90745c1a.js"><link rel="prefetch" href="/assets/js/36.ad08862a.js"><link rel="prefetch" href="/assets/js/37.dec32d64.js"><link rel="prefetch" href="/assets/js/38.3838a6c1.js"><link rel="prefetch" href="/assets/js/39.f4e4d4db.js"><link rel="prefetch" href="/assets/js/4.92869e3b.js"><link rel="prefetch" href="/assets/js/40.5fd3bb13.js"><link rel="prefetch" href="/assets/js/41.51a8fdd5.js"><link rel="prefetch" href="/assets/js/42.82c2e073.js"><link rel="prefetch" href="/assets/js/43.f0ea2a9e.js"><link rel="prefetch" href="/assets/js/44.671c08a7.js"><link rel="prefetch" href="/assets/js/45.5d15b10b.js"><link rel="prefetch" href="/assets/js/46.95005641.js"><link rel="prefetch" href="/assets/js/47.c9ab5796.js"><link rel="prefetch" href="/assets/js/48.43986c57.js"><link rel="prefetch" href="/assets/js/49.08b41247.js"><link rel="prefetch" href="/assets/js/5.5e32f39e.js"><link rel="prefetch" href="/assets/js/50.c11a05b9.js"><link rel="prefetch" href="/assets/js/51.639c8ff2.js"><link rel="prefetch" href="/assets/js/52.df138aff.js"><link rel="prefetch" href="/assets/js/53.39e24743.js"><link rel="prefetch" href="/assets/js/54.f975b5ab.js"><link rel="prefetch" href="/assets/js/55.3ac72402.js"><link rel="prefetch" href="/assets/js/56.ed625cdd.js"><link rel="prefetch" href="/assets/js/57.65af0446.js"><link rel="prefetch" href="/assets/js/58.3468c7f0.js"><link rel="prefetch" href="/assets/js/59.fea7da2c.js"><link rel="prefetch" href="/assets/js/6.3caf43c7.js"><link rel="prefetch" href="/assets/js/60.b978be38.js"><link rel="prefetch" href="/assets/js/61.17791f1f.js"><link rel="prefetch" href="/assets/js/62.3cd7c7e2.js"><link rel="prefetch" href="/assets/js/63.941cd4c6.js"><link rel="prefetch" href="/assets/js/64.7d62a354.js"><link rel="prefetch" href="/assets/js/65.0f35ef90.js"><link rel="prefetch" href="/assets/js/66.c9cd517d.js"><link rel="prefetch" href="/assets/js/67.ed261888.js"><link rel="prefetch" href="/assets/js/68.ab36cb55.js"><link rel="prefetch" href="/assets/js/69.e43a29c4.js"><link rel="prefetch" href="/assets/js/7.49459967.js"><link rel="prefetch" href="/assets/js/70.9c465e47.js"><link rel="prefetch" href="/assets/js/71.10344edd.js"><link rel="prefetch" href="/assets/js/72.9b399737.js"><link rel="prefetch" href="/assets/js/73.11e18677.js"><link rel="prefetch" href="/assets/js/74.9ab52ddf.js"><link rel="prefetch" href="/assets/js/75.15e0a627.js"><link rel="prefetch" href="/assets/js/76.433e942f.js"><link rel="prefetch" href="/assets/js/77.6f9979c4.js"><link rel="prefetch" href="/assets/js/78.c2cb7a93.js"><link rel="prefetch" href="/assets/js/79.7fc614fb.js"><link rel="prefetch" href="/assets/js/8.32c107dd.js"><link rel="prefetch" href="/assets/js/80.c75ae340.js"><link rel="prefetch" href="/assets/js/81.1d958700.js"><link rel="prefetch" href="/assets/js/82.9df67712.js"><link rel="prefetch" href="/assets/js/83.b3454e49.js"><link rel="prefetch" href="/assets/js/84.425e9e1a.js"><link rel="prefetch" href="/assets/js/85.d59ea930.js"><link rel="prefetch" href="/assets/js/86.4bdd04a1.js"><link rel="prefetch" href="/assets/js/87.121c124e.js"><link rel="prefetch" href="/assets/js/88.5a75898f.js"><link rel="prefetch" href="/assets/js/89.b87b375d.js"><link rel="prefetch" href="/assets/js/9.59e8ac2a.js"><link rel="prefetch" href="/assets/js/90.e51756a8.js"><link rel="prefetch" href="/assets/js/91.f8cae4b0.js"><link rel="prefetch" href="/assets/js/92.81b133ff.js"><link rel="prefetch" href="/assets/js/93.8350bd06.js"><link rel="prefetch" href="/assets/js/94.2d121b75.js"><link rel="prefetch" href="/assets/js/95.ef51b143.js"><link rel="prefetch" href="/assets/js/96.7d2163d3.js"><link rel="prefetch" href="/assets/js/97.a3cbbfc7.js"><link rel="prefetch" href="/assets/js/98.5ddb1d6b.js"><link rel="prefetch" href="/assets/js/99.5b21b4a2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.bb5b1957.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container have-rightmenu have-body-img"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="Teler的日常" class="logo"> <span class="site-name can-hide">Teler的日常</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">关于</a></div> <a href="https://github.com/fangweilong" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="/img/logo.png"> <div class="blogger-info"><h3>Teler</h3> <span>Day Day Up</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">关于</a></div> <a href="https://github.com/fangweilong" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>mybatis</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>lambda</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>maven</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>算法</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading open"><span>spark</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/354ff1/" aria-current="page" class="active sidebar-link">java-spark-朴素贝叶斯算法(naive-bayes)</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/pages/948a25/" class="sidebar-link">java-spark-主成分分析算法-pca</a></li><li><a href="/pages/a630f5/" class="sidebar-link">java-spark-kMeans算法</a></li><li><a href="/pages/ece276/" class="sidebar-link">java-spark的使用和配置</a></li><li><a href="/pages/682c0b/" class="sidebar-link">java-spark一元(多元)线性回归</a></li></ul></section></li><li><a href="/pages/79e33b/" class="sidebar-link">MinMaxNormalize规一化算法</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>spring boot</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>spring cloud</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>jsp</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>tomcat</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>cloudflare</span> <span class="arrow right"></span></p> <!----></section></li><li><a href="/pages/bf5f65/" class="sidebar-link">bootdo框架使用步骤总结</a></li><li><a href="/pages/f3e09f/" class="sidebar-link">filter拦截器获取http请求参数</a></li><li><a href="/pages/3ac4b1/" class="sidebar-link">java-String字符串split分割无效</a></li><li><a href="/pages/f38461/" class="sidebar-link">java表单提交，调用doNet的webservice乱码</a></li><li><a href="/pages/669f30/" class="sidebar-link">java访问webservice</a></li><li><a href="/pages/22420c/" class="sidebar-link">Long返回给前台在js中丢失精度</a></li><li><a href="/pages/a5eb49/" class="sidebar-link">MongoDB查询</a></li><li><a href="/pages/d31b31/" class="sidebar-link">NoSuchMethodError</a></li><li><a href="/pages/4589a8/" class="sidebar-link">PropertyNotFoundException</a></li><li><a href="/pages/311841/" class="sidebar-link">使用java生成微信随机加好友的链接</a></li><li><a href="/pages/6e84e0/" class="sidebar-link">连接chrome调试</a></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper bg-style-1"><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=java" title="分类" data-v-06225672>java</a></li><li data-v-06225672><a href="/categories/?category=%E7%AE%97%E6%B3%95" title="分类" data-v-06225672>算法</a></li><li data-v-06225672><a href="/categories/?category=spark" title="分类" data-v-06225672>spark</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/fangweilong" target="_blank" title="作者" class="beLink" data-v-06225672>Teler</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2020-12-10</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">java-spark-朴素贝叶斯算法(naive-bayes)<!----></h1>  <div class="theme-vdoing-content content__default"><h3 id="配置"><a href="#配置" class="header-anchor">#</a> 配置</h3> <p>配置请看我的<a href="https://blog.csdn.net/tutian2000/article/details/110948695" target="_blank" rel="noopener noreferrer">其他文章 点击跳转<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h3 id="spark官方文档"><a href="#spark官方文档" class="header-anchor">#</a> spark官方文档</h3> <p><a href="http://spark.apache.org/docs/latest/ml-classification-regression.html#linear-regression" target="_blank" rel="noopener noreferrer">点击跳转官方文档<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h3 id="其它文章"><a href="#其它文章" class="header-anchor">#</a> 其它文章</h3> <p>推荐一个在蚂蚁做算法的人写的文章，不过他的文章偏专业化，有很多数学学公式。我是看的比较懵。<a href="https://www.guofei.site/2017/11/22/regression.html" target="_blank" rel="noopener noreferrer">点击跳转<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h3 id="数据"><a href="#数据" class="header-anchor">#</a> 数据</h3> <h4 id="训练数据"><a href="#训练数据" class="header-anchor">#</a> 训练数据</h4> <p><img src="https://img-blog.csdnimg.cn/20201210115340670.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3R1dGlhbjIwMDA=,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p> <h4 id="预测数据"><a href="#预测数据" class="header-anchor">#</a> 预测数据</h4> <p><img src="https://img-blog.csdnimg.cn/20201210115400444.png" alt=""></p> <hr> <h3 id="实体类"><a href="#实体类" class="header-anchor">#</a> 实体类</h3> <p>用了swagger和lombok 不需要的可以删掉</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">import</span> <span class="token import"><span class="token namespace">io<span class="token punctuation">.</span>swagger<span class="token punctuation">.</span>annotations<span class="token punctuation">.</span></span><span class="token class-name">ApiModel</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">io<span class="token punctuation">.</span>swagger<span class="token punctuation">.</span>annotations<span class="token punctuation">.</span></span><span class="token class-name">ApiModelProperty</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">lombok<span class="token punctuation">.</span></span><span class="token class-name">Data</span></span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>validation<span class="token punctuation">.</span>constraints<span class="token punctuation">.</span></span><span class="token class-name">Max</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>validation<span class="token punctuation">.</span>constraints<span class="token punctuation">.</span></span><span class="token class-name">Min</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>validation<span class="token punctuation">.</span>constraints<span class="token punctuation">.</span></span><span class="token class-name">NotEmpty</span></span><span class="token punctuation">;</span>

<span class="token comment">/**
 * 分类算法传参
 * 比如决策树 随机森林 朴素贝叶斯
 *
 * @author teler
 * @date 2020-10-09
 */</span>

<span class="token annotation punctuation">@Data</span>
<span class="token annotation punctuation">@ApiModel</span><span class="token punctuation">(</span><span class="token string">&quot;朴素贝叶斯&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ClassifyEntity</span> <span class="token punctuation">{</span>
	<span class="token comment">/**
	 * 测试数据
	 */</span>
	<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span><span class="token string">&quot;测试数据&quot;</span><span class="token punctuation">)</span>
	<span class="token annotation punctuation">@NotEmpty</span><span class="token punctuation">(</span>message <span class="token operator">=</span> <span class="token string">&quot;参数不能为空&quot;</span><span class="token punctuation">)</span>
	<span class="token class-name">String</span> trainFilePath<span class="token punctuation">;</span>
	<span class="token comment">/**
	 * 数据路径
	 */</span>
	<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span><span class="token string">&quot;数据路径&quot;</span><span class="token punctuation">)</span>
	<span class="token annotation punctuation">@NotEmpty</span><span class="token punctuation">(</span>message <span class="token operator">=</span> <span class="token string">&quot;参数不能为空&quot;</span><span class="token punctuation">)</span>
	<span class="token class-name">String</span> dataFilePath<span class="token punctuation">;</span>

	<span class="token comment">/**
	 * 测试模型的数据比例
	 */</span>
	<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span><span class="token string">&quot;测试模型的数据比例&quot;</span><span class="token punctuation">)</span>
	<span class="token annotation punctuation">@Min</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> message <span class="token operator">=</span> <span class="token string">&quot;最小比例为0&quot;</span><span class="token punctuation">)</span>
	<span class="token annotation punctuation">@Max</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> message <span class="token operator">=</span> <span class="token string">&quot;最大比例为1&quot;</span><span class="token punctuation">)</span>
	<span class="token keyword">double</span> testDataPct<span class="token punctuation">;</span>

	<span class="token comment">/**
	 * 测试模型准确率阈值
	 */</span>
	<span class="token annotation punctuation">@ApiModelProperty</span><span class="token punctuation">(</span><span class="token string">&quot;测试模型准确率阈值&quot;</span><span class="token punctuation">)</span>
	<span class="token annotation punctuation">@Min</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> message <span class="token operator">=</span> <span class="token string">&quot;最小比例为0&quot;</span><span class="token punctuation">)</span>
	<span class="token annotation punctuation">@Max</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> message <span class="token operator">=</span> <span class="token string">&quot;最大比例为1&quot;</span><span class="token punctuation">)</span>
	<span class="token keyword">double</span> testThreshold<span class="token punctuation">;</span>

<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br></div></div><h3 id="算法实现"><a href="#算法实现" class="header-anchor">#</a> 算法实现</h3> <p>里面有些方法是为了保留小数 不需要的自己改</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>
<span class="token annotation punctuation">@Resource</span>
<span class="token keyword">private</span> <span class="token keyword">transient</span> <span class="token class-name">SparkSession</span> sparkSession<span class="token punctuation">;</span>

<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> <span class="token function">naiveBayes</span><span class="token punctuation">(</span><span class="token class-name">ClassifyEntity</span> record<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;========== 朴素贝叶斯算法开始 ==========&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token class-name">Dataset</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Row</span><span class="token punctuation">&gt;</span></span> source <span class="token operator">=</span> <span class="token function">getDataSetByHdfs</span><span class="token punctuation">(</span>record<span class="token punctuation">.</span><span class="token function">getTrainFilePath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token comment">//训练数据</span>
	map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;training&quot;</span><span class="token punctuation">,</span> <span class="token function">toList</span><span class="token punctuation">(</span>source<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token comment">//根据比例从数据源中随机抽取数据 训练数据和测试数据比例 建议设为0.8</span>
	<span class="token class-name">Dataset</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Row</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">[</span><span class="token punctuation">]</span> splits <span class="token operator">=</span> source<span class="token punctuation">.</span><span class="token function">randomSplit</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token keyword">double</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span>record<span class="token punctuation">.</span><span class="token function">getTestDataPct</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">1</span> <span class="token operator">-</span> record<span class="token punctuation">.</span><span class="token function">getTestDataPct</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
			<span class="token number">1234L</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token comment">//训练和测试数据</span>
	<span class="token class-name">Dataset</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Row</span><span class="token punctuation">&gt;</span></span> trainingData <span class="token operator">=</span> splits<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">cache</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token class-name">Dataset</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Row</span><span class="token punctuation">&gt;</span></span> testData <span class="token operator">=</span> splits<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">cache</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token class-name">NaiveBayes</span> nb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NaiveBayes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token class-name">NaiveBayesModel</span> model <span class="token operator">=</span> nb<span class="token punctuation">.</span><span class="token function">fit</span><span class="token punctuation">(</span>trainingData<span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token comment">//准确率</span>
	<span class="token class-name">Dataset</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Row</span><span class="token punctuation">&gt;</span></span> testResult <span class="token operator">=</span> model<span class="token punctuation">.</span><span class="token function">transform</span><span class="token punctuation">(</span>testData<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token class-name">Dataset</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Row</span><span class="token punctuation">&gt;</span></span> predictionAndLabels <span class="token operator">=</span> testResult<span class="token punctuation">.</span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string">&quot;prediction&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;label&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token class-name">MulticlassClassificationEvaluator</span> evaluator <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MulticlassClassificationEvaluator</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
			                                              <span class="token punctuation">.</span><span class="token function">setLabelCol</span><span class="token punctuation">(</span><span class="token string">&quot;label&quot;</span><span class="token punctuation">)</span>
			                                              <span class="token punctuation">.</span><span class="token function">setPredictionCol</span><span class="token punctuation">(</span><span class="token string">&quot;prediction&quot;</span><span class="token punctuation">)</span>
			                                              <span class="token punctuation">.</span><span class="token function">setMetricName</span><span class="token punctuation">(</span><span class="token string">&quot;accuracy&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token comment">//准确率</span>
	
	map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;accuracy&quot;</span><span class="token punctuation">,</span> <span class="token class-name">NumberUtil</span><span class="token punctuation">.</span><span class="token function">roundDown</span><span class="token punctuation">(</span>evaluator<span class="token punctuation">.</span><span class="token function">evaluate</span><span class="token punctuation">(</span>predictionAndLabels<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


	<span class="token comment">//计算结果</span>
	<span class="token class-name">Dataset</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Row</span><span class="token punctuation">&gt;</span></span> dataSource <span class="token operator">=</span> <span class="token function">getDataSetByHdfs</span><span class="token punctuation">(</span>record<span class="token punctuation">.</span><span class="token function">getDataFilePath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token class-name">Dataset</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Row</span><span class="token punctuation">&gt;</span></span> datasetRow <span class="token operator">=</span> model<span class="token punctuation">.</span><span class="token function">transform</span><span class="token punctuation">(</span>dataSource<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string">&quot;features&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;label&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;prediction&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token comment">//平滑指数</span>
	map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;smoothing&quot;</span><span class="token punctuation">,</span> <span class="token class-name">NumberUtil</span><span class="token punctuation">.</span><span class="token function">roundDown</span><span class="token punctuation">(</span>model<span class="token punctuation">.</span><span class="token function">getSmoothing</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token comment">//数据</span>
	map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;data&quot;</span><span class="token punctuation">,</span> <span class="token function">toList</span><span class="token punctuation">(</span>datasetRow<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> map<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br></div></div><hr> <h3 id="getdatasetbyhdfs方法"><a href="#getdatasetbyhdfs方法" class="header-anchor">#</a> getDataSetByHdfs方法</h3> <p>这个方法我与上面的方法放在一个类中，所以sparkSession没重复写</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
	 * 从hdfs中取数据
	 *
	 * @param dataFilePath 数据路径
	 * @return 数据集合
	 */</span>
	<span class="token keyword">private</span> <span class="token class-name">Dataset</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Row</span><span class="token punctuation">&gt;</span></span> <span class="token function">getDataSetByHdfs</span><span class="token punctuation">(</span><span class="token class-name">String</span> dataFilePath<span class="token punctuation">)</span> <span class="token punctuation">{</span>

		<span class="token comment">//屏蔽日志</span>
		<span class="token class-name">Logger</span><span class="token punctuation">.</span><span class="token function">getLogger</span><span class="token punctuation">(</span><span class="token string">&quot;org.apache.spark&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setLevel</span><span class="token punctuation">(</span><span class="token class-name">Level</span><span class="token punctuation">.</span><span class="token constant">WARN</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">Logger</span><span class="token punctuation">.</span><span class="token function">getLogger</span><span class="token punctuation">(</span><span class="token string">&quot;org.eclipse.jetty.server&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setLevel</span><span class="token punctuation">(</span><span class="token class-name">Level</span><span class="token punctuation">.</span><span class="token constant">OFF</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

		<span class="token class-name">Dataset</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Row</span><span class="token punctuation">&gt;</span></span> dataset<span class="token punctuation">;</span>
		<span class="token keyword">try</span> <span class="token punctuation">{</span>
			<span class="token comment">//我这里的数据是libsvm格式的 如果是其他格式请自行更改</span>
			dataset <span class="token operator">=</span> sparkSession<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token string">&quot;libsvm&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>dataFilePath<span class="token punctuation">)</span><span class="token punctuation">;</span>
			log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;获取数据结束 &quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;读取失败:{} &quot;</span><span class="token punctuation">,</span> e<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
		<span class="token keyword">return</span> dataset<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h3 id="tolist"><a href="#tolist" class="header-anchor">#</a> toList</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
	 * dataset数据转化为list数据
	 *
	 * @param record 数据
	 * @return 数据集合
	 */</span>
	<span class="token keyword">private</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Map</span><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> <span class="token function">toList</span><span class="token punctuation">(</span><span class="token class-name">Dataset</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Row</span><span class="token punctuation">&gt;</span></span> record<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;格式化结果数据集===============================&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Map</span><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> columns <span class="token operator">=</span> record<span class="token punctuation">.</span><span class="token function">columns</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Row</span><span class="token punctuation">&gt;</span></span> rows <span class="token operator">=</span> record<span class="token punctuation">.</span><span class="token function">collectAsList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Row</span> row <span class="token operator">:</span> rows<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> columns<span class="token punctuation">.</span>length<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
				<span class="token class-name">String</span> col <span class="token operator">=</span> columns<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
				<span class="token class-name">Object</span> rowAs <span class="token operator">=</span> row<span class="token punctuation">.</span><span class="token function">getAs</span><span class="token punctuation">(</span>col<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token class-name">String</span> val <span class="token operator">=</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">;</span>
				<span class="token comment">//如果是数组 </span>
				<span class="token comment">//这一段不需要的可以只留下else的内容</span>
				<span class="token keyword">if</span> <span class="token punctuation">(</span>rowAs <span class="token keyword">instanceof</span> <span class="token class-name">DenseVector</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
					<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token class-name">DenseVector</span><span class="token punctuation">)</span> rowAs<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">instanceof</span> <span class="token keyword">double</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
						val <span class="token operator">=</span> <span class="token class-name">ArrayUtil</span><span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>
								<span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token class-name">DenseVector</span><span class="token punctuation">)</span> rowAs<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
										<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>rowVal <span class="token operator">-&gt;</span> <span class="token class-name">NumberUtil</span><span class="token punctuation">.</span><span class="token function">roundDown</span><span class="token punctuation">(</span>rowVal<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">doubleValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
								<span class="token punctuation">,</span> <span class="token string">&quot;,&quot;</span><span class="token punctuation">)</span>
						<span class="token punctuation">;</span>
					<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
						val <span class="token operator">=</span> rowAs<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
					<span class="token punctuation">}</span>
				<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
					val <span class="token operator">=</span> rowAs<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token punctuation">}</span>

				obj<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>col<span class="token punctuation">,</span> val<span class="token punctuation">)</span><span class="token punctuation">;</span>
				log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;列：{},名：{},值：{}&quot;</span><span class="token punctuation">,</span> j<span class="token punctuation">,</span> col<span class="token punctuation">,</span> val<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">}</span>
			list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
		<span class="token keyword">return</span> list<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br></div></div></div></div>  <div class="page-edit"><div class="edit-link"><a href="https://github.com/fangweilong/edit/master/docs/01.java/04.算法/01.spark/01.java-spark-朴素贝叶斯算法(naive-bayes).md" target="_blank" rel="noopener noreferrer">编辑</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="tags"><a href="/tags/?tag=java" title="标签">#java</a><a href="/tags/?tag=spark" title="标签">#spark</a><a href="/tags/?tag=naive-bayes" title="标签">#naive-bayes</a><a href="/tags/?tag=%E6%9C%B4%E7%B4%A0%E8%B4%9D%E5%8F%B6%E6%96%AF" title="标签">#朴素贝叶斯</a></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2024-12-06, 10:03:39</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/1b457a/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">maven添加ojdbc6(11g)的jar包</div></a> <a href="/pages/948a25/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">java-spark-主成分分析算法-pca</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/1b457a/" class="prev">maven添加ojdbc6(11g)的jar包</a></span> <span class="next"><a href="/pages/948a25/">java-spark-主成分分析算法-pca</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/4ab140/"><div>
            利用cf大善人的 pages+AI gateway 搭建自己的AI平台
            <!----></div></a> <span class="date">12-06</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/104183/"><div>
            java playwright爬虫
            <!----></div></a> <span class="date">11-06</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/bdcbb5/"><div>
            连接chrome调试
            <!----></div></a> <span class="date">07-23</span></dt></dl> <dl><dd></dd> <dt><a href="/archives" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="fangweilong@outlook.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/fangweilong" title="GitHub" target="_blank" class="iconfont icon-github"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2021-2024
    <span> Teler 版权所有 | 我的博客小站。欢迎评论OR转发！</span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <div class="body-bg" style="background:url() center center / cover no-repeat;opacity:0.5;"></div> <!----> <!----></div><div class="global-ui"><div></div><canvas id="vuepress-canvas-cursor"></canvas><div></div></div></div>
    <script src="/assets/js/app.d41d74c0.js" defer></script><script src="/assets/js/2.023436e0.js" defer></script><script src="/assets/js/18.004165c1.js" defer></script>
  </body>
</html>
